#include "lnw.h"
#include <string.h>
void lnw_1ms_update_callback(void)
{
    
}

int lnw_queue_read(lnw_queue_t *queue, void *item)
{
    if((!queue->is_full)&&(queue->ptr_read==queue->ptr_write)) return -1;
    memcpy(item, queue->items+(queue->ptr_read*queue->item_size), queue->item_size);
    queue->ptr_read = (queue->ptr_read+1)%queue->total;
    return 0;
}

int lnw_queue_write(lnw_queue_t *queue, void *item, int flag)
{
    if(queue->is_full)
    {
        if(!flag) return -1;
        else 
        {
            queue->ptr_read = (queue->ptr_read+queue->total-1)%queue->total;
            memcpy(queue->items+(queue->ptr_read*queue->item_size), item, queue->item_size);
            queue->ptr_write = queue->ptr_read;
            return 0;
        }
    }
    else 
    {
        if(!flag) {
            memcpy(queue->items+(queue->ptr_write*queue->item_size), item, queue->item_size);
            queue->ptr_write = (queue->ptr_write+1)%queue->total;
            if(queue->ptr_read == queue->ptr_write) queue->is_full = 1;
            return 0;
        }
        else 
        {
            queue->ptr_read = (queue->ptr_read+queue->total-1)%queue->total;
            memcpy(queue->items+(queue->ptr_read*queue->item_size), item, queue->item_size);
            if(queue->ptr_read == queue->ptr_write) queue->is_full = 1;
            return 0;
        }
    }
    
}
